//
//  MyInformTabHead.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/11.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MyInformTabHead.h"

#define UserHeadHight screen_height*0.12-10
#import "AFNetworking.h"

@interface MyInformTabHead ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *userheadWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *userheadHight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *enterHight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *enterWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *linHight;

@end

@implementation MyInformTabHead

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        
    }
    return self;
}

-(void)awakeFromNib{
    
    self.linHight.constant = 0.5;
    
    self.enterWidth.constant = 12;
    
    self.enterHight.constant = 20;

    self.userheadHight.constant = UserHeadHight;
    self.userheadWidth.constant = UserHeadHight;
    radius(self.userHeader, (UserHeadHight)/2);
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tap];
}

-(void)tap{

    
    UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *ToPhone = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            [self takePhoto];
    }];
    
    UIAlertAction *ToImage = [UIAlertAction actionWithTitle:@"从相机获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            [self LocalPhoto];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [aler addAction:ToPhone];
    [aler addAction:ToImage];
    [aler addAction:cancel];
    
    [self.fatherctrl presentViewController:aler animated:YES completion:nil];


}

#pragma mark--调用相册
-(void)LocalPhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self.fatherctrl presentViewController:picker animated:YES completion:nil];
}

#pragma mark---调用相机
-(void)takePhoto{
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES; //设置拍照后的图片可被编辑
        picker.sourceType = sourceType; //资源类型为照相机
        [self.fatherctrl presentViewController:picker animated:YES completion:nil];
    }else {
        [self.fatherctrl ShowAlertWhthTitle:@"提示" Message:@"相机故障,请检查您的相机" CancelButtonTitle:@"好"];
    }
}

#pragma mark---相册相机回调协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //    [UIImagePNGRepresentation(portraitImg) writeToFile:@"/Users/diaoyuren/Desktop/压缩前.png" atomically:YES];
    UIImage *image = [Manager imageByScalingToMaxSize:portraitImg WhithMAXWIdth:100];//压缩处理
    
    NSData* imageData;
    NSString *head;
    
    if (portraitImg != nil) { //当图片不为空时显示图片并保存图片
        if (UIImagePNGRepresentation(image)) {
            imageData = UIImagePNGRepresentation(image);//返回为png图像。
            head = @".png";
        }else {
            imageData = UIImageJPEGRepresentation(image,1);//返回为JPEG图像。
            head = @".jpeg";
        }
    }
    NSString *name = [NSString stringWithFormat:@"vim_go%@",head];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"http://api.diaoyur.cn/app/index.php?version=4&module=uploadavatar" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:imageData name:@"filedata" fileName:name mimeType:@"application/octet-stream"];
        
        //        [imageData writeToFile:@"/Users/diaoyuren/Desktop/压缩后.png" atomically:YES];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (self.fatherctrl.imageChange) {
            self.fatherctrl.imageChange();
        }
        
        [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.fatherctrl.view];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [Manager GetHudWhithText:@"更换头像失败" AndFatherView:self.fatherctrl.view];
    }];
    
    [self.userHeader setImage:image]; //图片显示在界面上
    
    [picker dismissViewControllerAnimated:YES completion:nil];//关闭相册界面
}


@end
